<template>
	<view class="wdh-100 columnbox content">
		<view class="rowbox wdh-100 spb one-info">
			<view class="info-title">套餐名称</view>
			<view class="info-detail">
				<input v-model="name" placeholder="请输入" />
			</view>
		</view>
		<view class="rowbox wdh-100 spb one-info">
			<view class="info-title">实际支付</view>
			<view class="info-detail">
				<input type="digit" v-model="price" placeholder="请输入" />
			</view>
		</view>
		<view class="rowbox wdh-100 spb one-info">
			<view class="info-title">赠送金额</view>
			<view class="info-detail">
				<input type="digit" v-model="song" placeholder="请输入" />
			</view>
		</view>
		<view class="rowbox wdh-100 spb one-info">
			<view class="info-title">折扣</view>
			<view class="info-detail">
				<input type="digit" v-model="discount" placeholder="请输入" />
				<view class="unit">
					折
				</view>
			</view>
		</view>
		<view class="rowbox wdh-100">
			<view class="rowbox send-btn grey-btn" v-if="submitflag">确定</view>
			<view class="rowbox send-btn def-bgcol" v-else @click="submitInfo">确定</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				submitflag: false,
				price: '',
				song: '',
				discount: '',
				name: "",
				id: 0,
				updateflag: false,
				flag:''
			};
		},
		onLoad(options) {
			if (options.id) {
				this.id = parseInt(options.id)
				this.updateflag = true
				this.getDetail()
			}
			if(options.flag) {
				this.flag = options.flag
			}
		},
		methods: {
			getDetail() {
				this.$req.post('/shanghu/blockpacekageinfo', {
					id: this.id
				}).then(res => {
					this.name = res.data.data.name
					this.song = res.data.data.song
					this.price = res.data.data.price
					this.discount = res.data.data.discount
				})
			},
			submitInfo() {
				if (this.name == '') {
					uni.showToast({
						title: '请输入套餐名称',
						icon: 'none'
					})
				} else if (this.price == '') {
					uni.showToast({
						title: '请输入实际支付',
						icon: 'none'
					})
				} else if (this.song == '') {
					uni.showToast({
						title: '请输入赠送金额',
						icon: 'none'
					})
				} else {
					if (!this.submitflag) {
						this.submitflag = true
						uni.showLoading({

						})
						if (!this.updateflag) {
							this.$req.post('/shanghu/blockpacekageadd', {
								"shanghuid": uni.getStorageSync('userInfo').shanghuid,
								"price": parseFloat(this.price),
								"song": parseFloat(this.song),
								"discount": this.discount == '' ? 0 : parseFloat(this.discount),
								"name": this.name
							}).then(res => {
								let pages = getCurrentPages(); //获取所有页面栈实例列表
								let nowPage = pages[pages.length - 1]; //当前页页面实例
								let prevPage = pages[pages.length - 2]; //上一页页面实例
								prevPage.$vm.refreshflag = true
								uni.navigateBack()
							})
						} else {
							this.$req.post('/shanghu/blockpacekageupdate', {
								"shanghuid": uni.getStorageSync('userInfo').shanghuid,
								"price": parseFloat(this.price),
								"song": parseFloat(this.song),
								"discount": this.discount == '' ? 0 : parseFloat(this.discount),
								"name": this.name,
								"id": this.id
							}).then(res => {
								let pages = getCurrentPages(); //获取所有页面栈实例列表
								let nowPage = pages[pages.length - 1]; //当前页页面实例
								let prevPage = pages[pages.length - 2]; //上一页页面实例
								prevPage.$vm.refreshflag = true
								uni.navigateBack()
							})
						}
					}
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		padding: 20rpx 20rpx;
	}

	.one-info {
		padding: 32rpx 0;
		border-bottom: #f5f5f5 solid 1px;
		font-size: 28rpx;
	}

	.info-title {
		color: #5a5a5a;
	}

	.info-detail {
		display: flex;
		align-items: center;

		.unit {
			color: #999;
			padding-left: 5rpx;
		}

		text {
			margin-right: 10rpx;
		}

		input {
			font-size: 28rpx;
			text-align: right;
		}
	}

	.send-btn {
		margin-top: 100rpx;
		color: #fff;
		width: 600rpx;
		height: 100rpx;
		border-radius: 20rpx;
	}
</style>